#include "mutex.h"
#include "log.h"

Mutex::Mutex()
{
	int rc;
	if ( (rc = pthread_mutex_init(&_mutex, NULL)) != 0)
	{
		ELOG("pthread_mutex_init() failed: %d", rc);
		abort();
	}
}

Mutex::~Mutex()
{
	int rc;
	if ( (rc = pthread_mutex_destroy(&_mutex)) != 0)
	{
		ELOG("pthread_mutex_destroy() failed: %d", rc);
		abort();
	}
}

int Mutex::Lock()
{
	int rc;
	if ( (rc = pthread_mutex_lock(&_mutex)) != 0)
	{
		ELOG("pthread_mutex_lock() failed: %d\n", rc);
		abort();
		return -1;
	}
	return 0;
}

int Mutex::Unlock()
{
	int rc;
	if ( (rc = pthread_mutex_unlock(&_mutex)) != 0)
	{
		ELOG("pthread_mutex_unlock() failed: %d", rc);
		abort();
		return -1;
	}
	return 0;
}

bool Mutex::TryLock()
{
	return (pthread_mutex_trylock(&_mutex) == 0);
}